package com.wwtech.epp.ui.activity.mine;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.socks.library.KLog;
import com.wwtech.epp.R;
import com.wwtech.epp.application.AppConfig;
import com.wwtech.epp.base.BaseActivity;
import com.wwtech.epp.contain.ContainValue;
import com.wwtech.epp.entity.User;
import com.wwtech.epp.ui.activity.mine.component.DaggerMyInfoComponent;
import com.wwtech.epp.ui.activity.mine.contract.MyInfoContract;
import com.wwtech.epp.ui.activity.mine.module.MyInfoModule;
import com.wwtech.epp.ui.activity.mine.presenter.MyInfoPresenter;
import com.wwtech.epp.utils.PopWindowUtil;
import com.wwtech.epp.utils.ToastUtil;
import com.wwtech.epp.view.CustomPopWindow;
import com.wwtech.epp.view.GlideCircleTransform;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.mine
 * @Description: $description
 * @date 2017/08/31 21:07:09
 */

public class MyInfoActivity extends BaseActivity implements MyInfoContract.View, PopWindowUtil.OnSubmitClickListener {

    @Inject
    MyInfoPresenter mPresenter;
    @BindView(R.id.tv_avater)
    TextView tvAvater;
    @BindView(R.id.iv_avatar)
    ImageView ivAvatar;
    @BindView(R.id.nick)
    TextView nick;
    @BindView(R.id.tv_nick)
    TextView tvNick;
    @BindView(R.id.tv_bumen)
    TextView tvBumen;
    @BindView(R.id.tv_zhiwei)
    TextView tvZhiwei;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_email)
    TextView tvEmail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_my_info);
        ButterKnife.bind(this);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    protected void initData() {
        mPresenter.setUid(ContainValue.userLogin.getData().getUserId() + "");
        setTitle("个人信息");
//        mPresenter.getMyInfo(ContainValue.userLogin.getData().getUserId() + "");
        Glide.with(this)
                .load(ContainValue.userLogin.getData().getAvatar())
//                .skipMemoryCache(true)
//                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .transform(new GlideCircleTransform(this))
                .into(ivAvatar);
        tvBumen.setText(ContainValue.userLogin.getData().getDepartment());
        tvNick.setText(ContainValue.userLogin.getData().getNickName());
        tvPhone.setText(ContainValue.userLogin.getData().getPhone());
        tvZhiwei.setText(ContainValue.userLogin.getData().getOrgTitle());
        tvEmail.setText(ContainValue.userLogin.getData().getEmail());
    }

    @Override
    public void setMyInfo(User user) {
        Glide.with(this)
                .load(ContainValue.userLogin.getData().getAvatar())
                .transform(new GlideCircleTransform(this))
//                .skipMemoryCache(true)
//                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(ivAvatar);
        EventBus.getDefault().post("alertAvater");
        tvNick.setText(user.getNickName());
        tvPhone.setText(user.getPhone());
        tvZhiwei.setText(user.getOrgTitle());

    }

    @Override
    protected void setupActivityComponent() {
        DaggerMyInfoComponent
                .builder()
                .appComponent(((AppConfig) getApplication()).getApplicationComponent())
                .myInfoModule(new MyInfoModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void setPresenter(MyInfoContract.MyInfoContractPresenter presenter) {
        mPresenter = (MyInfoPresenter) presenter;
    }

    @Override
    public void showProgressDialog() {
        progressDialog.show();
    }

    @Override
    public void closeProgressDialog() {
        progressDialog.hide();
    }

    @OnClick({R.id.tv_avater, R.id.nick})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_avater:
                PopWindowUtil popWindowUtil = new PopWindowUtil();
                popWindowUtil.showCheckImagePopWindow(MyInfoActivity.this, tvAvater);
                popWindowUtil.setOnSubmitClickListener(MyInfoActivity.this);
                break;
            case R.id.nick:
                AlertDialog alertDialog = new AlertDialog.Builder(this).create();
                View view1 = View.inflate(this, R.layout.set_nick_dialog, null);
                alertDialog.setView(view1);
                alertDialog.setCancelable(true);
                EditText tv_title = (EditText) view1.findViewById(R.id.content);
                TextView cancal = (TextView) view1.findViewById(R.id.cancal);
                TextView sure = (TextView) view1.findViewById(R.id.sure);
                cancal.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        alertDialog.dismiss();
                    }
                });
                sure.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (tv_title.getText().toString().trim().equals("")) {
                            ToastUtil.showShort("昵称不能为空");
                            return;
                        }
                        alertDialog.dismiss();
                        MediaType textType = MediaType.parse("text/plain");
                        Map<String, RequestBody> map = new HashMap<>();
                        map.put("userId", RequestBody.create(textType, ContainValue.userLogin.getData().getUserId() + ""));
                        map.put("nickName", RequestBody.create(textType, tv_title.getText().toString() + ""));
                        mPresenter.setMyInfo(ContainValue.userLogin.getData().getUserId() + "", map);
                    }
                });
                alertDialog.show();
                break;
            default:
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        mPresenter.onActivityResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onSubmitClick(View v, String content) {
        switch (content) {
            case "拍照":
                KLog.i("从相机");
                mPresenter.fromCameraUpLoad();
                break;
            case "从相册选取":
                KLog.i("从相册");
                mPresenter.fromAlbumUpLoad();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        if (CustomPopWindow.onBackPressed()) {
            return;
        }
        super.onBackPressed();
    }
}